import { observable, action } from 'mobx'
import axios from '../../../common/request'
import  notificationWithIcon from '../../common/notificationWithIcon';
import modalDisplay from '../../common/informModal'

class ActivityListStore {
    @observable page={
        currentPage:1,
        pageSize:15
    }
    @observable total
    @observable activities=[]
    actions = {
        getActivity: action((param)=>{
            axios.get('/api/activity/list/'+param+"?pageNumber="+this.page.currentPage+"&pageSize="+this.page.pageSize)
                .then((response)=> {
                    if(response.data.status.code===1) {
                        this.activities=response.data.result.data
                        this.total=response.data.result.total
                    }
                    else{
                            notificationWithIcon('error',response.data.status.message,"")
                        }
                })
                .catch(function (error) {
                    notificationWithIcon('error',"获取活动列表失败",error.response)
                    console.log(error);
                });
        }),
        cancelActivity: action((id) => {
            axios.post('/api/activity/cancel',{activityId:id})
                .then((response)=> {
                    if(response.data.status.code===1){
                        modalDisplay('success','取消活动成功',"",null)
                        let index=this.activities.findIndex(item => item.id === id);
                        if(index!=-1)
                            this.activities.splice(index, 1)
                    }
                    else{
                        modalDisplay('error',response.data.status.message,"",null)
                    }
                })
                .catch(function (error) {
                    modalDisplay('error',"操作失败","",null)
                    console.log(error);
                });
        }),
        setCurrentPage: action((param,page) => {
            this.page={
                currentPage:page,
                pageSize:this.page.pageSize
            }
            this.actions.getActivity(param)
        }),
    }
}

export default new ActivityListStore()